class GadgetsController < ApplicationController

  def index
    @gadgets = Gadget.find(:all)
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @gadgets }
    end
  end
  
  
  def reinit
    Gadget.destroy_all
    Gadget.load() 
    render :text=>'Gadgets cleared and initialized!!!'
  end
  
  
  # Delete all gadgets
  def clear 
    Gadget.destroy_all
    render :text=>'Gadgets Cleared!!!'
  end


  # Repopulate Gadget database
  def init
    Gadget.load()                 
    render :text=>'Gadgets Initialized!!!'
  end


  def show
    @gadget = Gadget.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @gadget }
    end
  end


  def new
    @gadget = Gadget.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @gadget }
    end
  end


  def edit
    @gadget = Gadget.find(params[:id])
    @categories = Category.find(:all)
  end


  def create
    @gadget = Gadget.new(params[:gadget])
    respond_to do |format|
      if @gadget.save
        flash[:notice] = 'Gadget was successfully created.'
        format.html { redirect_to(@gadget) }
        format.xml  { render :xml => @gadget, :status => :created, :location => @gadget }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @gadget.errors, :status => :unprocessable_entity }
      end
    end
  end


  def update
    @gadget = Gadget.find(params[:id])
    respond_to do |format|
      if @gadget.update_attributes(params[:gadget])
        flash[:notice] = 'Gadget was successfully updated.'
        format.html { redirect_to(@gadget) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @gadget.errors, :status => :unprocessable_entity }
      end
    end
  end


  def destroy
    @gadget = Gadget.find(params[:id])
    @gadget.destroy
    respond_to do |format|
      format.html { redirect_to(gadgets_url) }
      format.xml  { head :ok }
    end
  end
end
